package com.gupaoedu.vip.mall.file.ceph;

import lombok.Data;
import org.javaswift.joss.client.factory.AccountConfig;
import org.javaswift.joss.client.factory.AccountFactory;
import org.javaswift.joss.client.factory.AuthenticationMethod;
import org.javaswift.joss.model.Account;
import org.javaswift.joss.model.Container;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Ceph配置类中创建`Account`账户和`Container`对象
 * 访问ceph，要提供账户和访问哪个容器中数据
 */
@Configuration
@ConfigurationProperties(prefix = "ceph")
@Data //yml配置自动注入到下面属性中
public class ContainerConfig {

    private String username;  //Ceph配置 主用户名：子用户名
    private String password;  //秘钥
    private String authUrl;   //ceph接口访问路径
    private String defaultContainerName;  //默认容器名字

    /**
     * Ceph账户信息配置
     * @return
     */
    @Bean
    public Account account(){
        AccountConfig config = new AccountConfig();
        config.setUsername(username);
        config.setPassword(password);
        config.setAuthUrl(authUrl);
        config.setAuthenticationMethod(AuthenticationMethod.BASIC);//给账户分配基础权限
        return new AccountFactory(config).createAccount();  //创建账户
    }

    /**
     * 容器对象-存储并上传文件
     * @return
     */
    @Bean
    public Container container(){
        Container newContainer = account().getContainer(defaultContainerName);
        if (!newContainer.exists()) return newContainer.create();
        else return newContainer;
    }

}
